using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using Antix;

namespace Antix.Data.Catalogue.Objects
{
    /// <summary>
    /// Database Index Collection
    /// </summary>
    public class DatabaseIndexCollection : DatabaseObjectCollection
    {
        #region Constructor

        /// <summary>
        /// Create collection
        /// </summary>
        internal DatabaseIndexCollection(DatabaseTable table)
       {
            Table = table;
        }

        #endregion

        #region Properties

        public DatabaseTable Table{get; private set;}

        #region Indexers

               /// <summary>
        /// Get item by name, if not found a new one is passed back
        /// </summary>
        new public DatabaseIndex this[string name]
        {
            get { return this[name, false]; }
        }

        /// <summary>
        /// Get item by name, if not found a new one is passed back
        /// </summary>
        public DatabaseIndex this[string name, bool create]
        {
            get
            {
                if (this.Contains(name))
                {
                    return (DatabaseIndex)base[name];
                }
                else if(create) { return this.Add(name); }

                return null;
            }
        }

        #endregion

        #endregion

        #region Methods

        #region Add

        /// <summary>
        /// Add a new index
        /// </summary>
        public DatabaseIndex Add(string name)
        {
            DatabaseIndex newItem = new DatabaseIndex(this.Table);
            newItem.Name = name;

            base.Add(newItem);

            return newItem;
        }

        /// <summary>
        /// Add a new index
        /// </summary>
        public DatabaseIndex Add(
            string name,
            string columns, bool isUnique)
        {
            DatabaseIndex newItem = this.Add(name);
            newItem.Columns = columns;
            newItem.IsUnique = isUnique;

            return newItem;
        }

        /// <summary>
        /// Add Existing item to collection
        /// </summary>
        /// <param name="item"></param>
        internal void Add(DatabaseIndex item)
        {
            base.Add(item);
        }

        #endregion

        #endregion
    }
}
